package com.example.springbooteasycode.controller;

import com.example.springbooteasycode.utils.qrcode.QRCodeGenerator;
import com.example.springbooteasycode.utils.qrcode.withText.QRCodeUtil;
import com.google.zxing.WriterException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

/**
 * 二维码生成controller
 * @author Csa
 * @version 1.0
 * @date 2024/10/10 14:07
 */
@RestController
public class QRCodeController {

    /**
     * 生成二维码
     *       请求方式 get：
     *            http://localhost:8080/generate-qr-code?url=https://www.baidu.com
     *            http://localhost:8080/generate-qr-code?url=https://property67-test.999time.cn/cms/notice
     * @param url
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/generate-qr-code", produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<byte[]> generateQRCode(@RequestParam("url") String url) throws Exception {
        int width = 300; // 二维码宽度
        int height = 300; // 二维码高度
        byte[] qrCodeImage = QRCodeGenerator.getQRCodeImage(url, width, height);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<>(qrCodeImage, headers, HttpStatus.OK);
    }

    /**
     * 生成二维码，带 字段text内容
     *           请求方式 get：
     *           http://localhost:8080/generateQrCode?url=https://www.baidu.com&number=8924062617520001259104&name=A5&location=1号楼-1单元-2层-201室
     * @param response
     * @param url
     * @param number
     * @param name
     * @param location
     * @throws IOException
     * @throws WriterException
     */
    @GetMapping("/generateQrCode")
    public void generateQrCode(HttpServletResponse response,
                               @RequestParam String url,
                               @RequestParam String number,
                               @RequestParam String name,
                               @RequestParam String location)
            throws IOException, WriterException {

        int qrWidth = 200; // 小二维码的宽度
        int qrHeight = 200; // 小二维码的高度
        int backgroundWidth = 300; // 背景的宽度
        int backgroundHeight = 300; // 背景的高度

        // 创建背景图像
        BufferedImage backgroundImage = QRCodeUtil.createBackground(backgroundWidth, backgroundHeight);

        // 在背景上生成二维码
        BufferedImage qrCodeImage = QRCodeUtil.generateQRCode(url, qrWidth, qrHeight);

        // 计算二维码在背景上的位置
        int xPosition = (backgroundWidth - qrWidth) / 2;
        int yPosition = 10; // 将二维码放置在背景的顶部

        // 将二维码放在背景的中心
        Graphics2D g2d = backgroundImage.createGraphics();
        g2d.drawImage(qrCodeImage, xPosition, yPosition, null);

        // 添加文本信息
        QRCodeUtil.addTextToBackground(g2d, "编号: " + number, Color.BLACK, 15, 230);
        QRCodeUtil.addTextToBackground(g2d, "名称: " + name, Color.BLACK, 15, 250);
        QRCodeUtil.addTextToBackground(g2d, "位置: " + location, Color.BLACK, 15, 270);

        g2d.dispose();

        // 将图像写入响应
        QRCodeUtil.saveImage(response, backgroundImage);
    }

    /**
     * 批量生成二维码，并且打包到zip文件中
     * http://localhost:8080/generateBatch
     *
     * [
     *     {"url": "https://example.com", "number": "001", "name": "Name1", "location": "Location1"},
     *     {"url": "https://example.org", "number": "002", "name": "Name2", "location": "Location2"}
     * ]
     * @param response
     * @param qrDataList
     * @throws IOException
     * @throws WriterException
     */
    @PostMapping("/generateBatch")
    public void generateBatch(HttpServletResponse response, @RequestBody List<Map<String, String>> qrDataList)
            throws IOException, WriterException {

        List<byte[]> images = new ArrayList<>();
        List<String> filenames = new ArrayList<>();

        int qrWidth = 200; // 小二维码的宽度
        int qrHeight = 200; // 小二维码的高度
        int backgroundWidth = 300; // 背景的宽度
        int backgroundHeight = 300; // 背景的高度

        for (Map<String, String> qrData : qrDataList) {
            String url = qrData.get("url");
            String number = qrData.get("number");
            String name = qrData.get("name");
            String location = qrData.get("location");

            byte[] imageBytes = QRCodeUtil.generateQRCodeImage(url, number, name, location, qrWidth, qrHeight, backgroundWidth, backgroundHeight);
            images.add(imageBytes);

            // 生成文件名
            String filename = "qrcode_" + number + ".png";
            filenames.add(filename);
        }

        // 将图像打包成ZIP文件
        QRCodeUtil.saveImagesAsZip(response, images, filenames);
    }

}
